#!/usr/bin/python

from hid import *

class USBMailNotifier():
  VENDOR_ID = 0x1294
  PRODUCT_ID = 0x1320
  BULK_OUT_EP = 0x02

  class Color():
    OFF = 0x00
    GREEN = 0x01
    RED = 0x02
    BLUE = 0x03
    AQUA = 0x04
    YELLOW = 0x05
    FUCHSIA = 0x06
    WHITE = 0x07

  def __init__(self):
    ret = hid_init()
    if ret != HID_RET_SUCCESS:
      sys.stderr.write("hid_init failed with return code %d.\n" % ret)

  def open(self):
    self.hid = hid_new_HIDInterface()
    matcher = HIDInterfaceMatcher()
    matcher.vendor_id = USBMailNotifier.VENDOR_ID
    matcher.product_id = USBMailNotifier.PRODUCT_ID
    ret = hid_force_open(self.hid, 0, matcher, 3)
    if ret != HID_RET_SUCCESS:
      sys.stderr.write("hid_force_open failed with return code %d.\n" % ret)

  def close(self):
    ret = hid_close(self.hid)
    if ret != HID_RET_SUCCESS:
      sys.stderr.write("hid_close failed with return code %d.\n" % ret)
    hid_cleanup()

  def set_color(self, color):
    buffer = chr(color) + "\x00\x00\x00\x00"
    ret = hid_interrupt_write(self.hid, USBMailNotifier.BULK_OUT_EP, buffer, 10000)
    if ret != HID_RET_SUCCESS:
      sys.stderr.write("hid_close failed with return code %d.\n" % ret)

